﻿using System;
using System.Runtime.InteropServices.Automation;

namespace ComToolkit
{
    //http://msdn.microsoft.com/en-us/library/x05fawxd%28VS.85%29.aspx
    public static class Registry
    {
        private static dynamic shell;

        static Registry()
        {
            shell = AutomationFactory.CreateObject("Wscript.Shell");
        }
 
        /// <summary>
        /// Retrieves the value associated with the specified name, in the specified
        /// registry key. If the name is not found in the specified key, returns a default
        /// value that you provide, or null if the specified key does not exist.
        /// </summary>
        /// <param name="keyName">
        /// The full registry path of the key, beginning with a valid registry root,
        /// such as "HKEY_CURRENT_USER".
        /// </param>
        /// <param name="valueName">
        /// The name of the name/value pair.
        /// </param>
        /// <param name="defaultValue">
        /// The value to return if valueName does not exist.
        /// </param>
        /// <returns>
        /// null if the subkey specified by keyName does not exist; otherwise, the value
        /// associated with valueName, or defaultValue if valueName is not found.
        /// </returns>
        public static object GetValue(string keyName, string valueName, object defaultValue)
        {
            object value = shell.RegRead(string.Format("{0}{1}", keyName, valueName));
            return value != null ? valueName : defaultValue;
        }

        public static void SetValue(string keyName, string valueName, object value, string type)
        {
            shell.RegWrite(string.Format("{0}{1}", keyName, type));
        } 
    }
}
